const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    store: [],
    card_id: 0,
    card: [],
    themeshow: false,
    timesshow: false,
    theme_id: 0,
    theme: '草木绿',
    locktime: false,
    locktimevalue: '长期',
    minDate: new Date().getTime(),
    columns: ['草木绿', '极光蓝', '中国红', '紫兰色', '晨曦黄'],
    color: ['green', 'blue', 'red', 'purple', 'yellow'],
    card_text: '请输入活动名称'
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onLoad: function (event) {
    let that = this;
    if (!app.util().isNull(event.id)) {
      that.setData({
        card_id: event.id
      })
      that.getCard(event.id);
    }
    that.getApi();
  },

  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    this.setData({
      skeleton: false,
    })
  },
  
  /**
    * 读取编辑的会员卡
    */
  getCard: function (card_id) {
    let that = this;
    app.api().Get("api/v1/allwin-card-editcard", { card_id: card_id }, function (result) {
      that.setData({
        card: result.data,
        card_text: result.data.name
      });

    })
  },
  /**
    * 读取我的订单
    */
  getApi: function () {
    let that = this;
    let checked = false;
    app.api().Get("api/v1/allwin-store-manage", function (result) {
      that.setData({
        store: result.data,
      });
    })
  },
  /**
   * 选择会员卡颜色
   */
  onColor: function (event) {
    let that = this;
    that.setData({
      card_text: event.detail
    })
  },
  /**
   * 创建会员卡
   */
  formSubmit: function (e) {
    let that = this;
    var data = e.detail.value;
    var isPost = false;
    var msg = '';
    if (app.util().isNull(data.theme)) {
      msg = '会员卡主题颜色必须填写'
    } else if (app.util().isNull(data.name)) {
      msg = '会员卡名称必须填写'
    } else if (app.util().isNull(data.amount)) {
      msg = '会员卡金额必须填写'
    } else if (app.util().isNull(data.price)) {
      msg = '付款金额必须填写'
    } else if (app.util().isNull(data.discount)) {
      msg = '用户消费用卡折扣必须填写'
    } else if (app.util().isNull(data.locktime)) {
      msg = '有效期必须填写'
    } else if (app.util().isNull(data.tips)) {
      msg = '使用须知必须填写'
    } else if (app.util().isNull(data.relation_l1)) {
      msg = '直推奖励金额必须填写'
    } else if (app.util().isNull(data.relation_l2)) {
      msg = '间推奖励金额必须填写'
    } else {
      isPost = true;
      wx.showLoading({ title: '提交中', mask: true })
      var parms = {
        theme: that.data.color[that.data.theme_id],
        name: data.name,
        amount: data.amount,
        price: data.price,
        locktime: data.locktime,
        discount: data.discount,
        islocktime: that.data.locktime,
        tips: data.tips,
        relation_l1: data.relation_l1,
        relation_l2: data.relation_l2
      }
      app.api().Post('api/v1/allwin-card-createcard',parms)
      wx.hideLoading();
    }
    if (!isPost) {
      wx.showModal({
        content: msg
      })
    }
  },
  /**
   * 编辑会员卡
   */
  editSubmit: function (e) {
    let that = this;
    var data = e.detail.value;
    var isPost = false;
    var msg = '';
    if (app.util().isNull(data.name)) {
      msg = '会员卡名称必须填写'
    } else if (app.util().isNull(data.amount)) {
      msg = '会员卡金额必须填写'
    } else if (app.util().isNull(data.price)) {
      msg = '付款金额必须填写'
    } else if (app.util().isNull(data.tips)) {
      msg = '使用须知必须填写'
    } else if (app.util().isNull(data.relation_l1)) {
       msg = '直推奖励金额必须填写'
    } else if (app.util().isNull(data.relation_l2)) {
       msg = '间推奖励金额必须填写'
    } else {
      isPost = true;
      wx.showLoading({ title: '提交中', mask: true });
      var card_id = that.data.card_id;
      var parms = {
        card_id: card_id,
        name: data.name,
        amount: data.amount,
        price: data.price,
        tips: data.tips,
        relation_l1: data.relation_l1,
        relation_l2: data.relation_l2
      }
      app.api().Post('api/v1/allwin-card-editcard?card_id=' + card_id,parms)
      wx.hideLoading();
    }
    if (!isPost) {
      wx.showModal({
        content: msg
      })
    }
  },
  /**
  * 修改安全状态
  */
  themePopup() {
    this.setData({
      themeshow: !this.data.themeshow
    });
  },
  onTheme(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      theme: value,
      theme_id: index
    });
    this.themePopup();
  },
  /**
   * 时间选项
   */
  onClickIcon() {
    this.setData({
      locktime: false,
      locktimevalue: '长期',
      timesshow: false
    });
  },
  timesPopup() {
    this.setData({
      timesshow: !this.data.timesshow
    });
  },
  onTimes(event) {
    var sjc = event.detail;
    var currentDate = app.util().timesToDate(sjc, 'Y-M-D');
    this.setData({
      locktimevalue: currentDate,
      locktime: true,
    });
    this.timesPopup();
  },
})